import api from './index';
import { alarmsMockData, getUseMockData } from './mockData.fixed';

// 获取告警列表
export const getAlarms = async (params = {}) => {  // 检查是否使用备用数据
  if (getUseMockData()) {
    
    // 备用数据过滤
    let filteredData = [...alarmsMockData.data];
    
    // 根据告警级别过滤
    if (params.alarmLevel) {
      filteredData = filteredData.filter(alarm => alarm.alarmLevel === params.alarmLevel);
    }
    
    // 根据处理状态过滤
    if (params.status) {
      filteredData = filteredData.filter(alarm => alarm.status === params.status);
    }
    
    // 模拟分页
    const page = params._page || 1;
    const limit = params._limit || 10;
    const startIndex = (page - 1) * limit;
    const endIndex = startIndex + limit;
    const paginatedData = filteredData.slice(startIndex, endIndex);
    
    // 构造响应对象
    return {
      data: paginatedData,
      headers: {
        'x-total-count': filteredData.length.toString()
      }
    };
  }
  
  // 实际API请求
  try {
    let url = '/alarms';
    const queryParams = [];
    
    if (params.alarmLevel) {
      queryParams.push(`alarmLevel=${params.alarmLevel}`);
    }
    
    if (params.status) {
      queryParams.push(`status=${params.status}`);
    }
    
    if (params._page) {
      queryParams.push(`_page=${params._page}`);
    }
    
    if (params._limit) {
      queryParams.push(`_limit=${params._limit}`);
    }
    
    if (queryParams.length > 0) {
      url += '?' + queryParams.join('&');
    }
    
    return await api.get(url);
  } catch (error) {
    console.error('获取告警列表失败:', error);
      // 出错时返回备用数据
    let filteredData = [...alarmsMockData.data];
    
    // 根据告警级别过滤
    if (params.alarmLevel) {
      filteredData = filteredData.filter(alarm => alarm.alarmLevel === params.alarmLevel);
    }
    
    // 根据处理状态过滤
    if (params.status) {
      filteredData = filteredData.filter(alarm => alarm.status === params.status);
    }
    
    // 模拟分页
    const page = params._page || 1;
    const limit = params._limit || 10;
    const startIndex = (page - 1) * limit;
    const endIndex = startIndex + limit;
    const paginatedData = filteredData.slice(startIndex, endIndex);
    
    // 构造响应对象
    return {
      data: paginatedData,
      headers: {
        'x-total-count': filteredData.length.toString()
      }
    };
  }
};

// 获取告警详情
export const getAlarmById = async (id) => {  // 检查是否使用备用数据
  if (getUseMockData()) {
    const alarm = alarmsMockData.data.find(a => a.alarmId === id);
    return { data: alarm || null };
  }
  
  // 实际API请求
  try {
    return await api.get(`/alarms/${id}`);  } catch (error) {
    console.error('获取告警详情失败，使用备用数据替代:', error);
    const alarm = alarmsMockData.data.find(a => a.alarmId === id);
    return { data: alarm || null };
  }
};

// 处理告警
export const processAlarm = async (processData) => {  // 检查是否使用备用数据
  if (getUseMockData()) {
    const alarmIndex = alarmsMockData.data.findIndex(a => a.alarmId === processData.alarmId);
    if (alarmIndex !== -1) {
      alarmsMockData.data[alarmIndex].status = 'processed';
      alarmsMockData.data[alarmIndex].processRecord = processData.processRecord;
    }
    return { data: { success: true } };
  }
  
  // 实际API请求
  try {
    return await api.patch(`/alarms/${processData.alarmId}`, {
      status: 'processed',
      processRecord: processData.processRecord
    });
  } catch (error) {
    console.error('处理告警失败', error);
    const alarmIndex = alarmsMockData.data.findIndex(a => a.alarmId === processData.alarmId);
    if (alarmIndex !== -1) {
      alarmsMockData.data[alarmIndex].status = 'processed';
      alarmsMockData.data[alarmIndex].processRecord = processData.processRecord;
    }
    return { data: { success: true } };
  }
};

// 忽略告警
export const ignoreAlarm = async (id) => {  // 检查是否使用备用数据
  if (getUseMockData()) {
    const alarmIndex = alarmsMockData.data.findIndex(a => a.alarmId === id);
    if (alarmIndex !== -1) {
      alarmsMockData.data[alarmIndex].status = 'ignored';
    }
    return { data: { success: true } };
  }
  
  // 实际API请求
  try {
    return await api.patch(`/alarms/${id}`, {
      status: 'ignored'
    });  } catch (error) {
    console.error('忽略告警失败', error);
    const alarmIndex = alarmsMockData.data.findIndex(a => a.alarmId === id);
    if (alarmIndex !== -1) {
      alarmsMockData.data[alarmIndex].status = 'ignored';
    }
    return { data: { success: true } };
  }
};

// 删除告警
export const deleteAlarm = async (id) => {  // 检查是否使用备用数据
  if (getUseMockData()) {
    const alarmIndex = alarmsMockData.data.findIndex(a => a.alarmId === id);
    if (alarmIndex !== -1) {
      alarmsMockData.data.splice(alarmIndex, 1);
      alarmsMockData.headers['x-total-count'] = (parseInt(alarmsMockData.headers['x-total-count']) - 1).toString();
    }
    return { data: { success: true } };
  }
  
  // 实际API请求
  try {
    return await api.delete(`/alarms/${id}`);  } catch (error) {
    console.error('删除告警失败', error);
    const alarmIndex = alarmsMockData.data.findIndex(a => a.alarmId === id);
    if (alarmIndex !== -1) {
      alarmsMockData.data.splice(alarmIndex, 1);
      alarmsMockData.headers['x-total-count'] = (parseInt(alarmsMockData.headers['x-total-count']) - 1).toString();
    }
    return { data: { success: true } };
  }
};
